package com.tarena.lbs.basic.controller;

import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.basic.pojo.param.TagParam;
import com.tarena.lbs.basic.pojo.po.TagLibraryPO;
import com.tarena.lbs.basic.pojo.query.TagQuery;
import com.tarena.lbs.basic.pojo.query.TagTypeQuery;
import com.tarena.lbs.basic.pojo.vo.TagLibraryVO;
import com.tarena.lbs.basic.pojo.vo.TagTypeVO;
import com.tarena.lbs.basic.pojo.vo.TagVO;
import com.tarena.lbs.basic.service.TagService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 与标签有关的业务接口
 */
@Tag(name = "标签数据管理")
@RestController
@Slf4j
public class TagController {

    @Autowired
    private TagService tagService;

    @Operation(summary = "文章标签")
    @GetMapping("/basic/tagLibrary/info/article/tags")
    public Result<Map<String, List<TagVO>>> getArticleTags(){
        Map<String,List<TagVO>> tags = tagService.getTagsByType(0);
        return new Result<>(tags);
    }

    //用户标签 和文章标签数据结构 查询逻辑完全相同 只是入参传递的是tag_type=1

    @Operation(summary = "用户标签")
    @GetMapping("/basic/tagLibrary/info/userList")
    public Result<Map<String, List<TagVO>>> getUserTags(){
        //考虑 既然标签结构 文章和用户相同 后续还会查询用户标签 tagType=0|1
        Map<String,List<TagVO>> tags = tagService.getTagsByType(1);
        return new Result<>(tags);
    }

    //给人群创建时,提供的标签可选数据

    @Operation(summary = "人群标签可选数据")
    @GetMapping("/basic/tagLibrary/info/user/tags")
    public Result<Map<String, List<TagVO>>> getGroupTags(){
        Map<String,List<TagVO>> tags = tagService.getTagsByType(1);
        return new Result<>(tags);
    }

    @Operation(summary = "新增标签")
    @PostMapping("/admin/basic/tag/addTag")
    public Result<Void> addTag(@RequestBody TagParam tagParam){
        log.debug("开始新增标签");
        tagService.addTag(tagParam);
        return Result.success();
    }

    @Operation(summary = "查询标签列表")
    @PostMapping ("/admin/basic/tag/pageList")
    public Result<PageResult<TagVO>> list(@RequestBody TagQuery  query){
        PageResult<TagVO> list = tagService.tagList(query);
        return new Result<>(list);
    }

    @Operation(summary = "查询标签分类")
    @PostMapping ("/admin/basic/tag/tagTypeList")
    public Result<List<TagTypeVO>> tagTypeList( @RequestBody TagTypeQuery query){
        log.debug("参数参数query:"+query);
        List<TagTypeVO> tagList=tagService.tagTypeList(query);
        return new Result<>(tagList);
    }

    @Operation(summary = "根据标签id查询标签信息")
    @GetMapping("/admin/basic/tag/getTagInfo/{id}")
    public Result<TagVO> getTagInfoById(@PathVariable("id") Integer id){
        TagVO vo = tagService.getTagInfoById(id);
        return new Result<>(vo);
    }

    @Operation(summary = "编辑标签")
    @PostMapping("/admin/basic/tag/edit")
    public Result<PageResult<TagVO>> editTag(@RequestBody TagParam param){
        tagService.editTag(param);
        TagQuery query = new TagQuery();
        query.setPageNo(param.getPageNo());
        query.setPageSize(param.getPageSize());
        PageResult<TagVO> voPage=tagService.pageList(query);
        return new Result<>(voPage);
    }

    @Operation(summary = "根据标签name查询标签信息")
    @GetMapping("/admin/basic/tag/getTagByName/{tagName}")
    public Result<TagVO> getTagByName(@PathVariable("tagName") String tagName){
        TagVO vo = tagService.getTagInfoByName(tagName);
        return new Result<>(vo);
    }









}
